#include <iostream>
#include <signal.h>
#include <enet/enet.h>
#include "../KMNetwork/KMNetClient.h"
#include "../KMNetwork/KMNetMsgIn.h"
#include "../testwx4/ChatProtocol.h"

class KMNetMsgIn;

class ChatClient : public KMNetClient
{
protected:
    virtual void processMessageFromServer(KMNetMsgIn &msg)
    {
        std::cout << "server send msg" << std::endl;
    }
};

bool doLoop = true;
void doExit(int)
{
    doLoop = false;
};

int main()
{
    signal(SIGINT, doExit);
    signal(SIGTERM, doExit);

    enet_initialize();

    ChatClient *client = new ChatClient();

    if( !client->connectToServer(std::string("localhost"),9999) )
    {
        std::cout << "connect to server failed" << std::endl;
    }
    else
    {
        std::cout << "connected to server" << std::endl;

        while(doLoop)
        {
            {
                system("pause");
                KMNetMsgOut msg;
                msg.writeId(CSLOGIN);
                msg.writeString(std::string("TEST"));
                client->send(msg);
            }

            client->update();
        }
        client->disconnect();
    }

    delete client;
    enet_deinitialize();
    std::cout << "exit.." << std::endl;
    return 0;
}
